package com.hp.his.basic.web;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.hp.his.basic.entity.UPMSPage;
import com.hp.his.basic.service.UPMSBaseManager;
import com.hp.his.core.orm.Page;
import com.hp.his.core.util.ServiceException;
import com.hp.his.core.util.web.struts2.CrudActionSupport;

/**
 * @author danny
 */
@Namespace("/basic")
@Results(
		{ @Result(name = CrudActionSupport.RELOAD, location = "page.action", type = "redirect")  }
)
public class PageAction extends CrudActionSupport<UPMSPage> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private UPMSBaseManager baseManager;
	
	private UPMSPage entity;
	
	private Page<UPMSPage> page = new Page<UPMSPage>(100);// 每页100条记录
	
	private Integer id;
	
	private String pageId;
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	public void setPageId(String pageId){
		this.pageId = pageId;
	}
	
	

	public Page<UPMSPage> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		// 设置默认排序方式
		//if (!page.isOrderBySetted()) {
			page.setOrderBy("pageId");
			page.setOrder(Page.ASC);
		//}
		
		page = baseManager.getAllPages(page);
		return SUCCESS;
	}



	@Override
	public UPMSPage getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}



	@Override
	public String save() throws Exception {
		try{
			baseManager.savePage(entity);
		} catch (ServiceException se){
			addActionMessage(se.getMessage());
			return INPUT;
		}
		return RELOAD;
	}
	
	public String search(){
		return "search";
	}



	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}



	@Override
	protected void prepareModel() throws Exception {
		
		if(id != null){
			entity = baseManager.getUPMSPageById(id);
		} else if(pageId!=null){
			entity = baseManager.getUPMSPageByPageId(pageId);
		}else{
			entity = new UPMSPage();
		}
	}

}
